//
//  LTLOAuth2.m
//  WeiBo
//
//  Created by Apple_Lzzy46 on 16/9/14.
//  Copyright © 2016年 LTL. All rights reserved.
//

#import "LTLOAuth2.h"
#import "LTLWelcome.h"

@interface LTLOAuth2 ()<UIWebViewDelegate>


/**
 *  web
 */
@property(nonatomic , weak )UIWebView *web;

@end

@implementation LTLOAuth2

/**
 *      ///信息
 client_id = "487089634"
 client_secret = "42339cdd2f4feee9903780fbc93e96aa"
 redirect_uri = "https://api.weibo.com/oauth2/default.html"
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    ///左边按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    ///右边按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"填充" style:UIBarButtonItemStyleDone target:self action:@selector(Tiancong)];
    
//    _client_id = @"487089634";
//    _client_secret = @"42339cdd2f4feee9903780fbc93e96aa";
//    _redirect_uri = @"https://api.weibo.com/oauth2/default.html";
    /// web视图
    UIWebView *web = [[UIWebView alloc]init];
    
    self.web = web;
    ///web视图代理
    web.delegate = self;
    ///视图
    self.view = web;
    [self Auth];
}
/**
 *  首地址
 */
-(void)Auth
{
    NSString *str = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",client_id,redirect_uri];
   //网址
    NSURL *url = [NSURL URLWithString:str];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [self.web loadRequest:request];
}
//web代理方法
-(void)webViewDidStartLoad:(UIWebView *)webView
{
    //指示器
    [SVProgressHUD show];

}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    //指示器
    [SVProgressHUD dismiss];

}


-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    
    //参数
    NSString *query = request.URL.query;
    
    NSString *index = @"code=";
    
    if ([query rangeOfString:index].location !=NSNotFound && query != nil ) {
        
        
        NSString *code =[query substringFromIndex:index.length];
        //获取授权过的Access Token
        [LTLNetworkRequest access_token:code success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
            //退出控制器
            [self back];
            
        }];
       // [[LTLObject alloc] access_tokenCode:code];
    }
    return true;
}
/**
 *  返回
 */
-(void)back
{
    [self.navigationController dismissViewControllerAnimated:YES completion:^{
        if ([LTLModel read].access_token) {
            
            UIWindow *win = [UIApplication sharedApplication].keyWindow;
            win.rootViewController = [[LTLWelcome alloc]init];
        }
    }];
}
//填充
-(void)Tiancong
{
    NSString *js = @"authZForm.userId.value = '1184676257@qq.com' , authZForm.passwd.value = 'yaoniLTL'";
    
    [_web stringByEvaluatingJavaScriptFromString:js];

}
@end
